package concurrent.lock.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * await/signal 必须在同步代码块中，await必须在signal前
 */
public class ConditionAwaitSignalDemo {


	public static void main(String[] args) {

		ReentrantLock lock = new ReentrantLock();

		Condition condition = lock.newCondition();

		new Thread(() -> {
			lock.lock();
			try {
				System.out.println(Thread.currentThread().getName() + "come in.");
				condition.await();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
			System.out.println(Thread.currentThread().getName() + "唤醒");
		}, "线程1").start();

		new Thread(() -> {
			lock.lock();
			try {
				condition.signal();
				System.out.println(Thread.currentThread().getName() + "通知");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}, "线程2").start();

	}
}
